package com.rc.saas.mini.service.ugs;

import com.rc.saas.mini.mapper.ugs.UgsMemberLevelRecordMapper;
import com.rc.saas.mini.model.ugs.TenantUgsLevelGrowing;
import com.rc.saas.mini.model.ugs.UgsMemberLevelRecord;
import com.rc.saas.mini.model.ugs.UgsMemberLevelRecordExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UgsMemberLevelRecordServiceImpl implements UgsMemberLevelRecordService {
    @Autowired
    private UgsMemberLevelRecordMapper ugsMemberLevelRecordMapper;

    @Override
    public UgsMemberLevelRecordMapper getMapper() {
        return ugsMemberLevelRecordMapper;
    }

    @Override
    public List<UgsMemberLevelRecord> listByGrowingTypeMemberIdIntraday(String tenantCode, Long levelGrowingId, Long memberId) {
        return ugsMemberLevelRecordMapper.listByGrowingTypeMemberIdIntraday(tenantCode, levelGrowingId, memberId);
    }

    @Override
    public void updateByGrowingType(String tenantCode, Long memberId, Integer countRecord, TenantUgsLevelGrowing tenantUgsLevelGrowing) {
        //签到次数作特殊处理
        if ("0".equals(tenantUgsLevelGrowing.getGrowingType())) {
            Integer sign = ugsMemberLevelRecordMapper.countByMemberId(tenantCode, memberId, tenantUgsLevelGrowing.getLevelGrowingId());
            countRecord = sign % tenantUgsLevelGrowing.getCycle().intValue();
        }
    }

    @Override
    public List<UgsMemberLevelRecord> listByGrowingTypeMemberId(String tenantCode, Long levelGrowingId, Long memberId) {
        UgsMemberLevelRecordExample example = new UgsMemberLevelRecordExample();
        example.createCriteria().andTenantCodeEqualTo(tenantCode).andMemberIdEqualTo(memberId).andGrowingTypeIdEqualTo(levelGrowingId);
        example.setOrderByClause(" created_time desc");
        return ugsMemberLevelRecordMapper.selectByExample(example);
    }

    @Override
    public UgsMemberLevelRecord selectByOrderId(Long orderId) {
        UgsMemberLevelRecordExample example=new UgsMemberLevelRecordExample();
        example.createCriteria().andOrderIdEqualTo(orderId);
        return ugsMemberLevelRecordMapper.selectByExample(example).stream().findFirst().orElseGet(()->null);
    }
}
